package sr.codemg.utils;

import org.springframework.util.StringUtils;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

public class StaticFunc {

    public static String date() {
        return new SimpleDateFormat("yyyyMMdd").format(new Date());
    }

    public static String dateTime() {
        return new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
    }

    public static String dateTime(Object date) {
        return new SimpleDateFormat("yyyyMMddHHmmss").format(date);
    }

    public static String dateFmtTime() {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
    }

    public static void page(Map<String, Object> params) {
        Integer page = Integer.parseInt(StaticFunc.ons(params.remove("page")));
        Integer size = Integer.parseInt(StaticFunc.ons(params.remove("size")));
        params.put("offset", (page - 1) * size);
        params.put("limit", page * size);
    }

    public static String[] strToArray(String array) {
        String[] arrs = new String[]{array};
        if (array.contains(",")) {
            arrs = array.split(",");
        }
        return arrs;
    }

    public static String ons(Object obj) {
        if (StringUtils.isEmpty(obj)) {
            return "";
        } else {
            return obj.toString();
        }
    }

    public static boolean delFile(File file) {
        if (!file.exists()) {
            return false;
        }

        if (file.isDirectory()) {
            File[] files = file.listFiles();
            for (File f : files) {
                delFile(f);
            }
        }
        return file.delete();
    }
}
